package com.guochenglang.www.controller;

import com.guochenglang.www.pojo.Base;
import com.guochenglang.www.pojo.InviteMessage;
import com.guochenglang.www.service.BaseService;
import com.guochenglang.www.service.InviteService;
import com.guochenglang.www.service.UserService;
import com.guochenglang.www.service.anotate.Affair;
import com.guochenglang.www.util.GetObject;
import com.guochenglang.www.util.MyDependency;
import com.guochenglang.www.web.result.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 邀请Controller层
 *
 * @author GuoChenglang
 */

public class InviteController {
    private static final InviteService INVITE_SERVICE = MyDependency.returnObject(InviteService.class);
    private static final UserService USER_SERVICE = MyDependency.returnObject(UserService.class);
    private static final BaseService BASE_SERVICE = MyDependency.returnObject(BaseService.class);

    /**
     * 邀请用户
     */
    public static Result inviteUser(HttpServletRequest req, HttpServletResponse resp) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException, IOException {
        //获取邀请人id
        Integer userId = GetObject.getValue(req, Integer.class, "userId");
        //获取数据库id
        Integer baseId = GetObject.getValue(req, Integer.class, "baseId");
        //获取邀请的用户名
        String userName = GetObject.getValue(req, String.class, "inviteUsername");
        //将该邀请添加进数据库
        int key = INVITE_SERVICE.addInvite(userId, baseId, userName);
        Result result = Result.result(key);
        //根据num判断是否添加成功
        if (key >= 0) {
            return result;
        } else {
            return Result.noResult();
        }
    }

    /**
     * 获得邀请个数
     */
    public static Result getInviteCount(HttpServletRequest req, HttpServletResponse resp) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户名
        String username = req.getParameter("username");
        //根据用户名进行查询
        int inviteCount = INVITE_SERVICE.getInviteCount(username);
        return Result.result(inviteCount);
    }

    /**
     * 展示邀请列表
     */
    public static Result showInvite(HttpServletRequest req, HttpServletResponse resp) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        Integer userId = Integer.valueOf(req.getParameter("userId"));
        //获得用户名
        String username = USER_SERVICE.getUser(userId).getUsername();
        //根据用户名搜索出邀请列表
        ArrayList<InviteMessage> inviteMessages = INVITE_SERVICE.showInviteMessages(username);
        ArrayList<Base> bases = new ArrayList<>();
        for (InviteMessage inviteMessage : inviteMessages) {
            //搜索出对应的base信息，送往前端显示
            bases.add(BASE_SERVICE.getBase(inviteMessage.getBaseId()));
        }
        return Result.result(bases);
    }

    /**
     * 接受邀请
     */
    public static Result acceptInvite(HttpServletRequest req, HttpServletResponse resp) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException, IOException {
        //获得知识库id
        Integer baseId = GetObject.getValue(req, Integer.class, "baseId");
        //获得邀请人用户id
        Integer userId = GetObject.getValue(req, Integer.class, "userId");
        //获得用户名
        String username = USER_SERVICE.getUser(userId).getUsername();
        //接受邀请
        int key = INVITE_SERVICE.acceptService(baseId, userId);
        return Result.result(key);
    }

    /**
     * 删除邀请
     */

    public static Result rejectInvite(HttpServletRequest req, HttpServletResponse resp) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException, IOException {
        //获得知识库id
        Integer baseId = GetObject.getValue(req, Integer.class, "baseId");
        //获得用户名
        String username = GetObject.getValue(req, String.class, "username");
        int num = INVITE_SERVICE.deleteInvite(username, baseId);
        return Result.result(num);
    }
}
